package dao

import (
	"context"
	"fmt"
	global "lgb24kcs.com/im/config"
	"lgb24kcs.com/im/internal/model"
	"lgb24kcs.com/im/utils"
)

var ctx context.Context

// SaveUser 使用Redis Hash数据类型存储用户信息
func SaveUser(user model.User) error {
	key := utils.GemSnowFlask(user.Name)
	//marshal, _ := json.Marshal(user)
	return global.App.Redis.HSet(ctx, key, "name", user.Name, "avatar", user.Avatar, "status", user.Status).Err()

}

// GetUser 获取用户信息
func GetUser(UserID string) (*model.User, error) {
	result, err := global.App.Redis.HGetAll(ctx, UserID).Result()
	if err != nil {
		return nil, err
	} else {
		if len(result) == 0 {
			return nil, fmt.Errorf("用户 %s 不存在", UserID)
		} else {

			user := &model.User{
				ID:     UserID,
				Name:   result["name"],
				Avatar: result["avatar"],
				Status: result["status"],
			}
			return user, nil
		}
	}
}
